package com.nb.customletternavigationbar;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

/**
 * Version 1.0
 * Describe:自定义字母导航
 */
class CustomLetterNavigationBar extends View {
    private Paint mPaint, mHighPaint;
    private int mTextSize = 14;
    private int mTextColor = Color.BLUE;
    private int mHighTextColor = Color.RED;
    private String[] mLetters = {"↑","☆","A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
            "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "#"};
    private String mCurrentLetter = mLetters[0];//默认选中第一个
    private int mItemHeight;//一个字母的高度

    public CustomLetterNavigationBar(Context context) {
        this(context, null);
    }

    public CustomLetterNavigationBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomLetterNavigationBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取自定义属性
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomLetterNavigationBar);
        mTextSize = array.getDimensionPixelSize(R.styleable.CustomLetterNavigationBar_letterTextSize, sp2px(mTextSize));
        mTextColor = array.getColor(R.styleable.CustomLetterNavigationBar_letterTextColor, mTextColor);
        mHighTextColor = array.getColor(R.styleable.CustomLetterNavigationBar_letterHighTextColor, mHighTextColor);
        array.recycle();

        //初始化画笔
        mPaint = getMyPaint(mTextColor);
        mHighPaint = getMyPaint(mHighTextColor);
    }

    private Paint getMyPaint(int color) {
        Paint paint = new Paint();

        paint.setTextSize(mTextSize);
        paint.setAntiAlias(true);
        paint.setDither(true);
        paint.setColor(color);
        return paint;
    }

    private int sp2px(int sp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp, getResources().getDisplayMetrics());
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //字母的宽度 +左右padding
        int width = (int) (mPaint.measureText("A") + getPaddingLeft() + getPaddingRight());
        int height = MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(width, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        mItemHeight = (getHeight() - getPaddingTop() - getPaddingBottom()) / mLetters.length;
        for (int i = 0; i < mLetters.length; i++) {
            int x = (int) (getWidth() / 2 - mPaint.measureText(mLetters[i]) / 2);
            int middleHeight = mItemHeight * i + getHeight() / mLetters.length / 2 + getPaddingTop();

            Paint.FontMetricsInt fontMetricsInt = mPaint.getFontMetricsInt();
            int dy = (fontMetricsInt.bottom - fontMetricsInt.top) / 2 - fontMetricsInt.bottom;

            int baseLine = middleHeight + dy;
            //当前滑动到的字母 高亮显示
            if (mCurrentLetter.equals(mLetters[i])) {
                canvas.drawText(mLetters[i], x, baseLine, mHighPaint);
            } else {
                canvas.drawText(mLetters[i], x, baseLine, mPaint);
            }

        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                //获取当前触摸的下标
                int currentIndex = getCurrentIndex(event.getY());

                //防止重绘
                if (!mCurrentLetter.equals(mLetters[currentIndex])) {
                    mCurrentLetter = mLetters[currentIndex];
                    if (mLetterTouchListener != null) {
                        mLetterTouchListener.onTouch(mCurrentLetter, true);
                    }
                    invalidate();

                }
                break;
            case MotionEvent.ACTION_UP:
                if (mLetterTouchListener != null) {
                    mLetterTouchListener.onTouch(mCurrentLetter, false);
                }
                break;
        }

        return true;
    }

    private int getCurrentIndex(float y) {
        int moveY = (int) y;
        int currentIndex = moveY / mItemHeight;

        //防止数组越界
        if (currentIndex < 0) {
            currentIndex = 0;
        }
        if (currentIndex > mLetters.length - 1) {
            currentIndex = mLetters.length - 1;
        }
        return currentIndex;
    }

    LetterTouchListener mLetterTouchListener;

    public void setOnLetterTouchListener(LetterTouchListener letterTouchListener) {
        this.mLetterTouchListener = letterTouchListener;
    }

    public interface LetterTouchListener {
        void onTouch(CharSequence letter, boolean isTouch);
    }
}
